
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;

namespace Common
{
	/// <summary>
	/// Description of PatientDao.
	/// </summary>
	public class PatientDao
	{
		private PatientDao()
		{
		}
		
		public static int insert(Patient p)
		{
			string sql="insert into patient(id,name,gender,birthday,telephone,address,birth_weight,childbirth,birth_situation)values(?,?,?,?,?,?,?,?,?);";
			OleDbParameter[] parameters = new OleDbParameter[9];
            parameters[0] = new OleDbParameter("@name", OleDbType.Integer);
            parameters[0].Value = p.Id;
			parameters[1]=new OleDbParameter("@name",OleDbType.VarChar,50);
			parameters[1].Value=p.Name;
            parameters[2] = new OleDbParameter("@gender", OleDbType.VarChar, 50);
            parameters[2].Value = p.Gender;
            parameters[3] = new OleDbParameter("@birthday", OleDbType.Date);
            parameters[3].Value = p.Birthday;
            parameters[4] = new OleDbParameter("@telephone", OleDbType.VarChar, 50);
            parameters[4].Value = p.Telephone;
            parameters[5] = new OleDbParameter("@address", OleDbType.VarChar, 50);
            parameters[5].Value = p.Address;
            parameters[6] = new OleDbParameter("@birth_weight", OleDbType.Double);
            parameters[6].Value = p.BirthWeight;
            parameters[7] = new OleDbParameter("@childbirth", OleDbType.VarChar, 50);
            parameters[7].Value = p.Childbirth;
            parameters[8] = new OleDbParameter("@birth_situation", OleDbType.VarChar, 50);
            parameters[8].Value = p.BirthSituation;

            return AccessDBUtil.ExecuteNonQuery(sql, parameters);
		}
		public static int deleteById(int id)
		{
			string sql="delete from patient where id=?";
			OleDbParameter[] parameters = new OleDbParameter[1];
			parameters[0] = new OleDbParameter("@id",OleDbType.Integer);
			parameters[0].Value=id;
			return AccessDBUtil.ExecuteNonQuery(sql,parameters);
		}
		public static int update(Patient p)
		{

            string sql = "update patient set name=?,gender=?,birthday=?,telephone=?,address=?,birth_weight=?,childbirth=?,birth_situation=? where id=?";
            OleDbParameter[] parameters = new OleDbParameter[9];
            parameters[0] = new OleDbParameter("@name", OleDbType.VarChar, 50);
            parameters[0].Value = p.Name;
            parameters[1] = new OleDbParameter("@gender", OleDbType.VarChar, 50);
            parameters[1].Value = p.Gender;
            parameters[2] = new OleDbParameter("@birthday", OleDbType.Date);
            parameters[2].Value = p.Birthday;
            parameters[3] = new OleDbParameter("@telephone", OleDbType.VarChar, 50);
            parameters[3].Value = p.Telephone;
            parameters[4] = new OleDbParameter("@address", OleDbType.VarChar, 50);
            parameters[4].Value = p.Address;

            parameters[5] = new OleDbParameter("@birth_weight", OleDbType.Double);
            parameters[5].Value = p.BirthWeight;
            parameters[6] = new OleDbParameter("@childbirth", OleDbType.VarChar, 50);
            parameters[6].Value = p.Childbirth;
            parameters[7] = new OleDbParameter("@birth_situation", OleDbType.VarChar, 50);
            parameters[7].Value = p.BirthSituation;
            parameters[8] = new OleDbParameter("@id", OleDbType.Integer);
            parameters[8].Value = p.Id;

			
			return AccessDBUtil.ExecuteNonQuery(sql,parameters);
			
			
		}
		public static Patient getById(int id)
		{
            string sql = "select id,name,gender,birthday,telephone,address,birth_weight,childbirth,birth_situation from patient where id=?";
			OleDbParameter[] parameters = new OleDbParameter[1];
			parameters[0] = new OleDbParameter("@id",OleDbType.Integer);
			parameters[0].Value=id;
			DataSet ds = AccessDBUtil.ExecuteQuery(sql,parameters);

            if (ds.Tables["ds"].Rows.Count > 0)
            {
                return Row2Patient(ds.Tables["ds"].Rows[0]);
            } return null;
		}

        public static Patient getByIdAndName(int id,string name)
        {
            string sql = "select id,name,gender,birthday,telephone,address,birth_weight,childbirth,birth_situation from patient where id=? and name=?";
            OleDbParameter[] parameters = new OleDbParameter[2];
            parameters[0] = new OleDbParameter("@id", OleDbType.Integer);
            parameters[0].Value = id;
            parameters[1] = new OleDbParameter("@name", OleDbType.VarChar);
            parameters[1].Value = name;
            DataSet ds = AccessDBUtil.ExecuteQuery(sql, parameters);

            if (ds.Tables["ds"].Rows.Count > 0)
            {
                return Row2Patient(ds.Tables["ds"].Rows[0]);
            } return null;
        }
		private static Patient Row2Patient(DataRow row)
		{
			Patient patient = new Patient();
			patient.Id = Int32.Parse(row["id"].ToString());
			patient.Name = row["name"].ToString();
            patient.Gender = row["gender"].ToString();
            patient.Birthday = DateTime.Parse(row["birthday"].ToString());
            patient.Telephone = row["telephone"].ToString();
            patient.Address = row["address"].ToString();
            patient.BirthWeight = Double.Parse(row["birth_weight"].ToString());
            patient.Childbirth = row["childbirth"].ToString();
            patient.BirthSituation = row["birth_situation"].ToString();

			return patient;
		}
		
	}
}
